package com.ossean.projectmanager.lasttabledao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ossean.projectmanager.model.Taggings;

public interface TaggingsDao {

	// 根据帖子id读取taggings表中与帖子相关联的taggings记录
	@Select("select * from taggings where taggable_id=#{memoId} and taggable_type='RelativeMemo'")
	public List<Taggings> getByMemoId(@Param("memoId") int memoId);

	// 根据项目id获得taggings
	@Select("SELECT * FROM taggings WHERE taggable_id = #{ospId} AND taggable_type = 'OpenSourceProject'")
	public List<Taggings> getByOspId(@Param("ospId") int ospId);

	// 向taggings表中插入由匹配帖子标签得到的新项目标签
	@Insert("insert into taggings (tag_id,taggable_id,taggable_type,disagree_num,context,created_at,tag_source) values (#{item.tag_id},#{item.taggable_id},#{item.taggable_type},#{item.disagree_num},#{item.context},now(),#{item.tag_source})")
	public void insertTaggings(@Param("item") Taggings item);

	// 查看taggings表中是否存在要查询的标签
	@Select("select * from taggings where tag_id=#{item.tag_id} and taggable_type=#{item.taggable_type} and taggable_id=#{item.taggable_id}")
	public List<Taggings> findTaggings(@Param("item") Taggings item);

	// 修改disagree_num值
	@Update("update taggings set disagree_num=#{value} where taggable_id=#{taggable_id} AND taggable_type=#{taggable_type} AND tag_id=#{tag_id} ")
	public void updateDisagreeNum(@Param("value") int value,
			@Param("taggable_id") int taggable_id,
			@Param("taggable_type") String taggable_type,
			@Param("tag_id") int tag_id);

	// 获取disagree_num值
	@Select("select disagree_num from taggings where tag_id=#{tag_id} and taggable_id=#{taggable_id} and taggable_type=#{taggable_type}")
	public int getDisagreeNum(@Param("tag_id") int tag_id,
			@Param("taggable_id") int taggable_id,
			@Param("taggable_type") String taggable_type);
}
